package com.ruoyi.question.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.DtAnswer;
import com.ruoyi.question.service.IDtAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 答案Controller
 *
 * @author llc
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/question/answer")
public class DtAnswerController extends BaseController {
    @Autowired
    private IDtAnswerService dtAnswerService;

    /**
     * 查询答案列表
     */
    @PreAuthorize("@ss.hasPermi('question:answer:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtAnswer dtAnswer) {
        startPage();
        List<DtAnswer> list = dtAnswerService.selectDtAnswerList(dtAnswer);
        return getDataTable(list);
    }

    /**
     * 导出答案列表
     */
    @PreAuthorize("@ss.hasPermi('question:answer:export')")
    @Log(title = "答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtAnswer dtAnswer) {
        List<DtAnswer> list = dtAnswerService.selectDtAnswerList(dtAnswer);
        ExcelUtil<DtAnswer> util = new ExcelUtil<DtAnswer>(DtAnswer.class);
        util.exportExcel(response, list, "答案数据");
    }

    /**
     * 获取答案详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:answer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dtAnswerService.selectDtAnswerById(id));
    }

    /**
     * 新增答案
     */
    @PreAuthorize("@ss.hasPermi('question:answer:add')")
    @Log(title = "答案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtAnswer dtAnswer) {
        return toAjax(dtAnswerService.insertDtAnswer(dtAnswer));
    }

    /**
     * 修改答案
     */
    @PreAuthorize("@ss.hasPermi('question:answer:edit')")
    @Log(title = "答案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtAnswer dtAnswer) {
        return toAjax(dtAnswerService.updateDtAnswer(dtAnswer));
    }

    /**
     * 删除答案
     */
    @PreAuthorize("@ss.hasPermi('question:answer:remove')")
    @Log(title = "答案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dtAnswerService.deleteDtAnswerByIds(ids));
    }
}
